﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;
using GalaSoft.MvvmLight.Messaging;
using Microsoft.Win32;

namespace Miris.HumanMilkAnalyser.WPF.Shared
{
    /// <summary>
    /// Provide some shared methods used within View
    /// </summary>
    public static class ViewHelper
    {
        /// <summary>
        /// Clean in View.
        /// </summary>
        public static void Cleanup(object view)
        {
            // Cleanup in View.
            Messenger.Default.Unregister(view);

            MyViewModelBase viewModel = null;

            // Cleanup in ViewModel.
            if (view is WindowBase)
            {
                viewModel = ((WindowBase)view).DataContext as MyViewModelBase;
            }
            else if (view is PageBase)
            {
                viewModel = ((PageBase)view).DataContext as MyViewModelBase;
            }
            else if (view is UserControlBase)
            {
                viewModel = ((UserControlBase)view).DataContext as MyViewModelBase;
            }

            if (viewModel != null)
            {
                viewModel.Cleanup();
            }
        }

        /// <summary>
        /// Create a instance of OpenFileDialog with default setting values.
        /// </summary>
        /// <param name="filter">The filter string that determines what types of files are displayed.</param>
        /// <param name="title">The text that appears in the title bar of a file dialog.</param>
        /// <param name="initialDirectory">The initial directory that is displayed by a file dialog.</param>
        /// <returns>A instance of OpenFileDialog with default setting values.</returns>
        public static OpenFileDialog CreateOpenFileDialog(string filter, string title, string initialDirectory)
        {
            var openFileDialog = new OpenFileDialog();
            openFileDialog.CheckFileExists = true;
            openFileDialog.CheckPathExists = true;
            openFileDialog.Title = title;
            openFileDialog.Filter = filter;
            openFileDialog.InitialDirectory = initialDirectory;

            return openFileDialog;
        }

        /// <summary>
        /// Create a instance of SaveFileDialog with default setting values.
        /// </summary>
        /// <param name="filter">The filter string that determines what types of files are displayed.</param>
        /// <param name="defaultExt">The default extension.</param>
        /// <param name="title">The text that appears in the title bar of a file dialog.</param>
        /// <returns>A instance of SaveFileDialog with default setting values.</returns>
        public static SaveFileDialog CreateSaveFileDialog(string filter, string defaultExt, string title)
        {
            var saveFileDialog = new SaveFileDialog();
            saveFileDialog.AddExtension = true;
            saveFileDialog.Filter = filter;
            saveFileDialog.Title = title;
            saveFileDialog.DefaultExt = defaultExt;

            return saveFileDialog;
        }

        /// <summary>
        /// Run the provided Action by current Dspatcher.
        /// </summary>
        /// <param name="action">The provided action will be invoked.</param>
        public static void RunAction(Action action)
        {
            if (Application.Current.Dispatcher.CheckAccess())
            {
                action.Invoke();
            }
            else
            {
                Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal, action);
            }
        }
    }
}
